/**
 * Created by 张乐平 on 8/2 0002.
 */

jQuery(function () {
    function getTree() {
        // Some logic to retrieve, or generate tree structure
        return [
            {
                text: "用户管理",
                href: "/system/authorization/users"

            },
            {
                text: "角色管理",
                href: "/system/authorization/roles/query",
                state: {
                    checked: true,
                    disabled: false,
                    expanded: true,
                    selected: true
                }
            },
            {
                text: "授权管理",
                href: "/system/authorization/perms/query"
            }
        ];
    }

    $('#tree').treeview({enableLinks:true,data: getTree()});

    $('#myModal').modal('hide');

    $('#myModal').on('hide.bs.modal', function () {
        //alert('嘿，我听说您喜欢模态框...');
    })
});

var confirmDelUser = function (roleName, roleId) {
    $("#delRoleName").text(roleName);
    $("#myModal-del").modal("show");
    $("#doDel").one("click", function () {
        window.location = "./del?roleId="+roleId;
    });
};
var updateNum=0;
var showUser = function (roleId) {
    $.ajax({
        type: "post",
        url: "./query",
        data:{
          roleId:roleId
        },
        dataType: "json",
        async: true,
        success: function (data) {
            $("#addRoleName-update").val(data.addRoleName);
            $("#roleId").val(data.roleId);
            $("#addDescription-update").val(data.addDescription);
            var a=data.list;
            if(a.length) {
                updateNum = a.length;
            }
            $("#myUpdateTable").empty();
            for(var i=0;i< a.length;i++){
                $("#myUpdateTable").append("<tr id='trUpdate"+i+"'><td><input type='text' name='list["+i+"].dataType' id='listUpdate"+i+"dataType'  class='form-control'></td> <td><input type='text' name='list["+i+"].operation' id='listUpdate"+i+"operation'  class='form-control'></td> <td><input type='text' name='list["+i+"].dataDomain' id='listUpdate"+i+"dataDomain'  class='form-control' ></td> <td><span class='glyphicon glyphicon-trash' onclick='delUpdate("+i+")' style='margin-bottom: .5em;'/></td> </tr>");
                $("#listUpdate"+i+"dataType").val(a[i].dataType==null?"":a[i].dataType);
                $("#listUpdate"+i+"operation").val(a[i].operation==null?"":a[i].operation);
                $("#listUpdate"+i+"dataDomain").val(a[i].dataDomain==null?"":a[i].dataDomain);
            }
            $("#myModal-update").modal("show");
        }
    });
};
function addUpdatetr(){
    $("#myUpdateTable").append("<tr id='trUpdate"+updateNum+"'><td><input type='text' name='list["+updateNum+"].dataType' id='listUpdate"+updateNum+"dataType' class='form-control'></td> <td><input type='text' name='list["+updateNum+"].operation' id='listUpdate"+updateNum+"operation' class='form-control'></td> <td><input type='text' name='list["+updateNum+"].dataDomain' id='listUpdate"+updateNum+"dataDomain' class='form-control'></td> <td><span class='glyphicon glyphicon-trash' onclick='delUpdate("+updateNum+")' style='margin-bottom: .5em;'/></td> </tr>");
    updateNum++;
}
function delUpdate(num){
    $("#trUpdate"+num).remove();
}

var addnum = 1;
function addtr() {
    $("#myAddTable").append("<tr id='tr" + addnum + "'><td>" +
        "<input type='text' name='list[" + addnum + "].dataType' id='list[" + addnum + "].dataType' class='form-control'/>" +
        "</td> <td>" +
        "<input type='text' name='list[" + addnum + "].operation' id='list[" + addnum + "].operation' class='form-control'/></td> " +
        "<td><input type='text' name='list[" + addnum + "].dataDomain' id='list[" + addnum + "].dataDomain' class='form-control'/></td> " +
        "<td><span class='glyphicon glyphicon-trash' onclick='del(" + addnum + ")' style='margin-bottom: .5em;'/></td> </tr>");
    addnum++;
}
function del(num) {
    $("#tr" + num).remove();
}